//
//  LoginViewControlier.swift
//  amicool
//
//  Created by mario-yc on 2017/6/12.
//  Copyright © 2017年 mario-yc. All rights reserved.
//

import UIKit


class LoginTableViewController: UITableViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false
        
        // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
        // self.navigationItem.rightBarButtonItem = self.editButtonItem()
    }
    
    @IBOutlet weak var username: UITextField!
    
    
    
    @IBOutlet weak var password: UITextField!
   
    
    
    @IBAction func login(_ sender: AnyObject) {
        if self.username.text?.characters.count == 0{
            pompAlert(msg:"没有输入用户名")
            
        }else if self.password.text?.characters.count == 0{
            pompAlert(msg:"没有输入密码")
        }
        else{
            let parameters:NSDictionary = ["username":self.username.text!,"password":self.password.text!]
             AMNetworkMngTool.shared.AMNetwork_Login(parameters,block:{(flag) in
                if flag == "1"{
                    //异步处理
                    DispatchQueue.main.asyncAfter(deadline: DispatchTime.now()+0.5, execute:
                        {
                            //登录成功，切换到主界面，界面跳转
                            //self.pompAlert(msg: "登录成功")
                            UIApplication.shared.keyWindow?.rootViewController = MainTabbarController()
                    })
                }
                else{
                    self.pompAlert(msg: "登录不成功")
                }
            })
        }
        
    }



    //
    func pompAlert(msg:String){
        let alert = UIAlertController(title:"提示", message:msg,preferredStyle:.alert)
        alert.addAction(UIAlertAction(title:"确定",style:.cancel,handler:nil))
        self.present(alert,animated: true,completion: nil)
    }
    //
    func textFieldShouldReturn(_ texeField:UITextField)->Bool{
        self.username.resignFirstResponder()
        self.password.resignFirstResponder()
        return true
    }
    
    //
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 80.0
    }
    
    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        if section == 0{
            return self.view.frame.size.height/10
        }
        else{
            return 60.0
        }
    }
    
        func registerComplete(_ _notification:Notification)
    {
        let data = _notification.userInfo! as NSDictionary
        let username = data["username"]! as! String
        self.username.text = username
    }
    
       
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
    /*
     // MARK: - Navigation
     
     // In a storyboard-based application, you will often want to do a little preparation before navigation
     override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
     // Get the new view controller using segue.destinationViewController.
     // Pass the selected object to the new view controller.
     }
     */
}
